package beikai.com.assistant.service.impl;

import beikai.com.assistant.common.domain.params.VillageParams;
import beikai.com.assistant.common.exception.EleException;
import beikai.com.assistant.common.util.R;
import beikai.com.assistant.dao.mapper.VillageInfoMapper;
import beikai.com.assistant.dao.model.VillageInfoModel;
import beikai.com.assistant.service.IVillageInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 小区信息 服务实现类
 * </p>
 *
 * @author beikai
 * @since 2021-12-19
 */
@Service
public class VillageInfoServiceImpl extends ServiceImpl<VillageInfoMapper, VillageInfoModel> implements IVillageInfoService {

    @Override
    public JSONObject addVillage(VillageParams villageParams) {
        int count = count(Wrappers.lambdaQuery(new VillageInfoModel())
                .eq(VillageInfoModel::getIsDelete, false)
                .eq(VillageInfoModel::getName, villageParams.getName())
        );
        if (count > 0) {
            throw new EleException("小区已存在");
        }
        VillageInfoModel villageInfoModel = new VillageInfoModel();
        villageInfoModel.setName(villageParams.getName());
        if (save(villageInfoModel)) {
            return R.success();
        }
        throw new EleException("添加小区信息失败");
    }
}
